//
//  PLWebViewController.m
//  PointLegend
//
//  Created by 黄卫 on 17/2/24.
//  Copyright © 2017年 huangwei. All rights reserved.//

#import "TYWebViewController.h"
#import "UIWebView+AFNetworking.h"
#import "GJTipView.h"

@interface TYWebViewController ()<UIWebViewDelegate>
{
    UILabel *flagLabel;
}
@property (nonatomic, strong) UIView *errorTipView;
@end

@implementation TYWebViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //进度条
    _progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 62, Screen_Width, 2)];
    _progressView.progressViewStyle = UIProgressViewStyleBar;
    _progressView.progressTintColor = RGB(54, 192, 16);
    [_progressView setProgress:0.6 animated:YES];
    [self.baseNavBar addSubview:_progressView];
    
    //WebView
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, Screen_Width, Screen_Height-64)];
    _webView.scrollView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    _webView.delegate = self;
    [self.view addSubview:_webView];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:(_urlString)]];
    request.timeoutInterval = 30;
    
    WEAKSELF
    NSProgress *progress;
    [_webView loadRequest:request progress:&progress success:^NSString * _Nonnull(NSHTTPURLResponse * _Nonnull response, NSString * _Nonnull HTML) {
        NSString *title = [weakSelf.webView stringByEvaluatingJavaScriptFromString:@"document.title"];
        if (title.length>0) {
            weakSelf.title = title;
        }
        [weakSelf setProgress:1];
        return HTML;
    } failure:^(NSError * _Nonnull error) {
        [weakSelf setProgress:-2];
    }];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
    [_webView stopLoading];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(removeProgressView) object:nil];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [MBProgressHUD showLoadingWithView:self.webView text:@""];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [MBProgressHUD hideLoadingFromView:self.webView];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [MBProgressHUD hideLoadingFromView:self.webView];
    
    WEAKSELF
    UIView *errorTipView = [[GJTipView alloc] initWithFrame:webView.bounds tipImage:ImageNamed(@"login_icon") mianTip:error.domain detailTip:nil touchBlock:^{
        [weakSelf reloadWebView];
    }];
    [self.webView addSubview:errorTipView];
    self.errorTipView = errorTipView;
}

#pragma mark - 重新加载

- (void)reloadWebView
{
    [self.errorTipView removeFromSuperview];
    self.errorTipView = nil;
    [self.webView reload];
}

#pragma mark Others

- (void)setProgress:(CGFloat)progress
{
    if (progress==-2) {
//        [PLLoadingFlag showFlagAttachedToView:self.view withState:PLLoadingStateFail];
        [_progressView removeFromSuperview];
    }
    if (progress < 0.6) {
        return;
    }
    [_progressView setProgress:progress animated:YES];
    if (progress==1) {
        [self performSelector:@selector(removeProgressView) withObject:nil afterDelay:0.8];
        return;
    }
}

- (void)removeProgressView
{
    [_progressView removeFromSuperview];
}


- (void)dealloc
{
    NSLog(@"%s",__func__);
}


@end
